package com.newland.starpos.web.exception.hander;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import com.newland.starpos.project.exception.AbstractHttpException;
import com.newland.starpos.project.exception.BadRequestException;
import com.newland.starpos.project.http.ResponseModel;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@ControllerAdvice
public class ControllerExceptionHander {

	private static final String errorMessage = "服务器异常,请稍候再试";

	@ResponseBody
	@ExceptionHandler(BadRequestException.class)
	public ResponseModel<?> handerBadRequestException(BadRequestException hander) {
		// logException(hander);
		log.error("hand exception:[{}], httpStatus:[{}], message:[{}]", hander.getClass().getSimpleName(),
				hander.getHttpStatus().value(), hander.getMessage());

		return ResponseModel.error(hander.getHttpStatus(), hander.getMessage());
	}

	@ResponseBody
	@ExceptionHandler(Exception.class)
	public ResponseModel<?> handerAllException(Exception hander) {
		if (hander instanceof AbstractHttpException) {
			log.error("hand exception:[{}], httpStatus:[{}], message:[{}]", hander.getClass().getSimpleName(),
					((AbstractHttpException) hander).getHttpStatus().value(), hander.getMessage());

			return ResponseModel.error(((AbstractHttpException) hander).getHttpStatus(), hander.getMessage());
		}
		log.error("hand exception:[{}], message:[{}]", hander.getClass().getSimpleName(), hander.getMessage());

		return ResponseModel.error(HttpStatus.INTERNAL_SERVER_ERROR, errorMessage);
	}

}
